//
//  EpointMailSginInfoListViewController.m
//  EpointFrame5
//
//  Created by wangzhigang on 14-5-20.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "EpointMailSginInfoListViewController.h"
#import "SignInfoModel.h"
#import "MailSginInfoCell.h"
#import "SSCheckBoxView.h"
#import "Mail_ShouHui_Task.h"
#import "Mail_SignLst_Task.h"
#import "UIPopoverListView.h"

#define Mail_ShouHuiTaskGuid 1
#define getsignmailtaskguid 2

@interface EpointMailSginInfoListViewController ()<UIPopoverListViewDataSource, UIPopoverListViewDelegate>{
    NSMutableArray *dataArray;
    NSMutableString *ReceiveUserGuidList;
    bool isCheckAll;
    NSArray *operationList;

    NSString *MailGuid;
    UIPopoverListView *poplistview;

}

@end

@implementation EpointMailSginInfoListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title=@"签收信息";
    MailGuid =[initParam objectForKey:@"MailGuid"];
    dataArray=[initParam objectForKey:@"dataArray"];
    
    ReceiveUserGuidList=[[NSMutableString alloc]initWithString:@""];
    isCheckAll=false;
    [self setRightBarButtonItemWithTitle:@"操作" action:@selector(doAction)];
    operationList = @[@"全选",@"收回"];
}
-(void)doAction{
    [self showPopverListView];
}

#pragma mark tableview相关方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *XDNoticeCell1 = @"MailSginInfoCell";
    MailSginInfoCell *cell = (MailSginInfoCell *)[tableView dequeueReusableCellWithIdentifier:XDNoticeCell1];
    if (cell==nil) {
        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"MailSginInfoCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    SignInfoModel *m = [dataArray objectAtIndex:indexPath.row];
    NSMutableString *str = [[NSMutableString alloc]initWithString:@""];
    [str appendString:m.SignUserDispName];
    if (m.SignDate.length==0) {
        [str appendString:@"(未签收)\n\n"];
        SSCheckBoxViewStyle style = kSSCheckBoxViewStyleGlossy;
        CGRect loc = CGRectMake(20, 4, 40, 30);
        SSCheckBoxView *cbox = [[SSCheckBoxView alloc]initWithFrame:loc style:style checked:isCheckAll];
        [cbox setTag:indexPath.row];
        cbox.stateChangedBlock= ^(SSCheckBoxView *v)
        {
            NSLog(@"index=%i",v.tag);
            SignInfoModel *m = [dataArray objectAtIndex:v.tag];
            if (v.checked) {
                NSLog(@"checked");
                [ReceiveUserGuidList appendFormat:@"%@;",m.SignUserGuid];
            }else{
                NSLog(@"unchecked");
                
                //删除未选中的人guid
                NSString* str=[ReceiveUserGuidList stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@;",m.SignUserGuid] withString:@""];
                //清空被回收人的guid
                [ReceiveUserGuidList deleteCharactersInRange:NSMakeRange(0, ReceiveUserGuidList.length)];
                [ReceiveUserGuidList appendString:str];
                //在全选状态时 更新按钮名称
                
                operationList = @[@"全选",@"收回"];
                isCheckAll=NO;//重置标志
            }
            NSLog(@"ReceiveUserGuidList=%@",ReceiveUserGuidList);
        };
        [cbox setText:@""];
        [cell addSubview:cbox];
    }
    else
    {
        [str appendString:[NSString stringWithFormat:@"(%@)\n\n",m.SignDate]];
    }
    cell.sginMsg.text=str;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

#pragma mark 回收未签收邮件
- (IBAction)getBackUnReadMailAction:(id)sender {
    if (ReceiveUserGuidList.length==0) {
        [self toastMessage:@"没有选择被收回的对象"];
        return;
    }
    NSMutableDictionary *params = [self passParam];
    [params setObject:MailGuid forKey:@"MailGuid"];
    [params setObject:ReceiveUserGuidList forKey:@"ReceiveUserGuidList"];
    
    [self startTask:@"Mail_ShouHui_Task" withParams:params withTaskId:Mail_ShouHuiTaskGuid];
    [self showMBPHUD];
}
-(void)refreshUIWithThreadGuid:(int)taskId andData:(NSObject *)obj{
    
    [self hideMBPHUD];
    if(Mail_ShouHuiTaskGuid ==taskId)
    {
        NSString *bs = (NSString *)obj;
        if ([self validateXML:bs]) {
            [self toastMessage:@"邮件回收成功"];
            NSMutableDictionary *params = self.passParam;
            [params setObject:MailGuid forKey:@"MailGuid"];
            [self startTask:@"Mail_SignLst_Task" withParams:params withTaskId:getsignmailtaskguid];
            [self showMBPHUD];
        }
    }else if(getsignmailtaskguid ==taskId)
    {
        NSString *bs = (NSString *)obj;
        dataArray = [Mail_SignLst_Task getList:bs];
        if (dataArray.count==0) {
            [self toastMessage:@"没有签收信息！"];
            [self popBack];
        }else{
            [self.sginInfoList reloadData];
        }
    }

}

-(void)showPopverListView{
    NSString *title = @"操作";
    
    CGFloat pWidth = AppWidth - 60;
    CGFloat pHeight = TitleHeight + 40*2;
    CGFloat pTop = (AppHeight - SystemTakeHeight - BottomBarHeight - pHeight)*0.5;
//    if (poplistview==nil) {
         poplistview = [[UIPopoverListView alloc] initWithFrame:CGRectMake(30, pTop, pWidth, pHeight)];
        poplistview.delegate = self;
        poplistview.datasource = self;
        poplistview.listView.scrollEnabled = TRUE;
        poplistview.parentView = self.view;
        //    poplistview.tag = tag;
        [poplistview setTitle:title];
//    }
   
  
    [poplistview show];
}


#pragma mark - UIPopoverListViewDataSource

- (UITableViewCell *)popoverListView:(UIPopoverListView *)popoverListView
                    cellForIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                   reuseIdentifier:identifier];
    
    NSString *m = [operationList objectAtIndex:indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:18.0];
    cell.textLabel.text = m;
    return cell;
}

- (NSInteger)popoverListView:(UIPopoverListView *)popoverListView
       numberOfRowsInSection:(NSInteger)section {
    
    return operationList.count;
}

#pragma mark - UIPopoverListViewDelegate
- (void)popoverListView:(UIPopoverListView *)popoverListView
     didSelectIndexPath:(NSIndexPath *)indexPath {
//    NSString* m = [operationList objectAtIndex:indexPath.row];
    if (indexPath.row==0) {
        //全选
        [self checkAll];
    }else{
        //收回
        [self getBackUnReadMailAction];
    }
}
-(void)checkAll{
    if (isCheckAll) {
        //清空被回收人的guid
        [ReceiveUserGuidList deleteCharactersInRange:NSMakeRange(0, ReceiveUserGuidList.length)];
        isCheckAll=NO;
        operationList = @[@"全选",@"收回"];
    }else{
        //清空被回收人的guid
        [ReceiveUserGuidList deleteCharactersInRange:NSMakeRange(0, ReceiveUserGuidList.length)];
        for (int i=0; i<dataArray.count; i++) {
            SignInfoModel *m = [dataArray objectAtIndex:i];
            //未签收邮件过滤 可被回收
            if (m.SignDate.length==0) {
                [ReceiveUserGuidList appendFormat:@"%@;",m.SignUserGuid];
            }
        }
        operationList = @[@"取消",@"收回"];
        isCheckAll=YES;
    }
    NSLog(@"ReceiveUserGuidList=%@",ReceiveUserGuidList);
    [self.sginInfoList reloadData];
}

- (CGFloat)popoverListView:(UIPopoverListView *)popoverListView
   heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}
- (void)dismissFromParentView:(UIPopoverListView *)popoverListView {
    
}

#pragma mark 回收未签收邮件
- (void)getBackUnReadMailAction {
    if (ReceiveUserGuidList.length==0) {
        [self toastMessage:@"没有选择被收回的对象"];
        return;
    }
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:MailGuid forKey:@"MailGuid"];
    [params setObject:ReceiveUserGuidList forKey:@"ReceiveUserGuidList"];
    
    [self startTask:@"Mail_ShouHui_Task" withParams:params withTaskId:Mail_ShouHuiTaskGuid];
    [self showMBPHUD];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
